package main

import (
	"fmt"
	"os"
	"os/signal"
	"sync"
	"syscall"
	"vpn/guard/network"
)

func main() {
	udpConn, err := network.NewUDPConn(51820)
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	sigCh := make(chan os.Signal, 1)
	wg := sync.WaitGroup{}
	wg.Add(1)
	go func() {
		defer wg.Done()
		buffer := make([]byte, 65535)
		for {
			n, addr, err := udpConn.ReadFromUDP(buffer)
			if err != nil {
				fmt.Println("error:", err)
				return
			}
			fmt.Println("read:", n, addr)
			udpConn.WriteToUDP([]byte("hello client"), addr)
		}
	}()
	// Wait for shutdown signal
	signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
	fmt.Println("waiting for signal")
	<-sigCh
	udpConn.Close()
	wg.Wait()
}
